package com.rxh.util;

import com.rxh.pojo.payment.TradeObject;
import org.apache.commons.codec.digest.DigestUtils;
import com.rxh.utils.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Base64;

/**
 * Created with IntelliJ IDEA.
 * User: 陈俊雄
 * Date: 2018/5/30
 * Time: 9:26
 * Project: Management
 * Package: com.rxh.util
 */
public class QuickTestUtils {
    // 测试内嵌标识
    public static final int INLINE_PAY = 0;
    // 测试跳转标识
    public static final int CROSS_PAY = 1;

    private static final String UTF_8 = "UTF-8";

    private static Logger logger = LoggerFactory.getLogger(QuickTestUtils.class);

    public static String inlinePay(TradeObject tradeObject) {
        Document document = common(tradeObject);
        Element element = document.getRootElement();
        Element cardNo = element.addElement("CardNo");
        cardNo.addText(tradeObject.getCardNo());
        Element issuingBank = element.addElement("Issue");
        if (StringUtils.isNotBlank(tradeObject.getIssue())) {
            issuingBank.addText(tradeObject.getIssue());
        }
        Element securityNum = element.addElement("SecurityNum");
        securityNum.addText(tradeObject.getSecurityNum());
        Element expireYear = element.addElement("ExpireYear");
        expireYear.addText(String.valueOf(tradeObject.getExpireYear()));
        Element expireMonth = element.addElement("ExpireMonth");
        expireMonth.addText(String.valueOf(tradeObject.getExpireMonth()));
        return Base64.getEncoder().encodeToString(document.asXML().getBytes());
    }

    public static String crossPay(TradeObject tradeObject) {
        Document document = common(tradeObject);
        return Base64.getEncoder().encodeToString(document.asXML().getBytes());
    }

    public static Document common(TradeObject tradeObject) {
        Document document = DocumentHelper.createDocument();
        document.setXMLEncoding(UTF_8);
        Element element = document.addElement("Order");
        Element merNo = element.addElement("MerNo");
        merNo.setText(String.valueOf(tradeObject.getMerNo()));
        Element billNo = element.addElement("BillNo");
        billNo.setText(tradeObject.getBillNo());
        Element freight = element.addElement("Freight");
        freight.setText(String.valueOf(tradeObject.getFreight()));
        Element amount = element.addElement("Amount");
        amount.setText(String.valueOf(tradeObject.getAmount()));
        Element fee = element.addElement("Fee");
        fee.setText(String.valueOf(tradeObject.getFee()));
        Element currency = element.addElement("Currency");
        currency.setText(tradeObject.getCurrency());
        Element billFirstName = element.addElement("BillFirstName");
        billFirstName.setText(tradeObject.getBillFirstName());
        Element billLastName = element.addElement("BillLastName");
        billLastName.setText(tradeObject.getBillLastName());
        Element billPhone = element.addElement("BillPhone");
        billPhone.setText(tradeObject.getBillPhone());
        Element billEmail = element.addElement("BillEmail");
        billEmail.setText(tradeObject.getBillEmail());
        Element billAddress = element.addElement("BillAddress");
        billAddress.setText(tradeObject.getBillAddress());
        Element billCity = element.addElement("BillCity");
        billCity.setText(tradeObject.getBillCity());
        Element billState = element.addElement("BillState");
        billState.setText(tradeObject.getBillState());
        Element billCountry = element.addElement("BillCountry");
        billCountry.setText(tradeObject.getBillCountry());
        Element billZip = element.addElement("BillZip");
        billZip.setText(tradeObject.getBillZip());
        Element shipCountry = element.addElement("ShipCountry");
        shipCountry.setText(tradeObject.getShipCountry());
        Element shipState = element.addElement("ShipState");
        shipState.setText(tradeObject.getShipState());
        Element shipCity = element.addElement("ShipCity");
        shipCity.setText(tradeObject.getShipCity());
        Element shipAddress = element.addElement("ShipAddress");
        shipAddress.setText(tradeObject.getShipAddress());
        Element shipZip = element.addElement("ShipZip");
        shipZip.setText(tradeObject.getShipZip());
        Element shipFirstName = element.addElement("ShipFirstName");
        shipFirstName.setText(tradeObject.getShipFirstName());
        Element shipLastName = element.addElement("ShipLastName");
        shipLastName.setText(tradeObject.getShipLastName());
        Element shipPhone = element.addElement("ShipPhone");
        shipPhone.setText(tradeObject.getShipPhone());
        Element shipEmail = element.addElement("ShipEmail");
        shipEmail.setText(tradeObject.getShipEmail());
        Element lang = element.addElement("Lang");
        lang.setText(tradeObject.getLang());
        Element ip = element.addElement("Ip");
        ip.setText(tradeObject.getIp());
        Element payMode = element.addElement("PayMode");
        payMode.setText(tradeObject.getPayMode());
        Element payType = element.addElement("PayType");
        if (StringUtils.isNotBlank(tradeObject.getPayType())) {
            payType.setText(tradeObject.getPayType());
        }
        Element returnURL = element.addElement("ReturnURL");
        returnURL.setText(tradeObject.getReturnURL());
        Element notifyURL = element.addElement("NotifyURL");
        notifyURL.setText(tradeObject.getNotifyURL());
        Element remark = element.addElement("Remark");
        remark.setText(tradeObject.getRemark());

        Element goodList = element.addElement("GoodList");
        tradeObject.getProductDetails().forEach(productDetail -> {
            Element goods = goodList.addElement("Goods");
            Element goodId = goods.addElement("GoodId");
            goodId.setText(productDetail.getGoodId());
            Element goodName = goods.addElement("GoodName");
            goodName.setText(productDetail.getGoodName());
            Element qty = goods.addElement("Qty");
            qty.setText(String.valueOf(productDetail.getQty()));
            Element price = goods.addElement("Price");
            price.setText(String.valueOf(productDetail.getPrice()));
        });

        Element md5Info = element.addElement("Md5Info");
        String string = String.valueOf(tradeObject.getMerNo()) +
                tradeObject.getBillNo() +
                tradeObject.getFreight() +
                tradeObject.getAmount() +
                tradeObject.getFee() +
                tradeObject.getCurrency() +
                tradeObject.getReturnURL() +
                tradeObject.getBillEmail() +
                tradeObject.getMd5Info();
        md5Info.addText(DigestUtils.md5Hex(string));
        return document;
    }
}
